// Copyright (c) 2012, yuno. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of contributors
//    may be used to endorse or promote products derived from this software
//    without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
package sugarmintcandy.widgetwindow;

import java.lang.ref.WeakReference;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

public class ColorGridCellView extends View {
    private static WeakReference<Paint> sPaint1Ref;
    private static WeakReference<Paint> sPaint2Ref;
    private Paint mPaint3;

    public ColorGridCellView(Context context) {
	super(context);
	mPaint3 = new Paint();
    }

    public int getColor() {
	return mPaint3.getColor();
    }

    public void setColor(int color) {
	color |= 0xff000000;
	mPaint3.setColor(color);
	invalidate();
    }

    @Override
    protected int getSuggestedMinimumWidth() {
	float density = getResources().getDisplayMetrics().density;
	int width = Math.round(32 * density);
	return Math.max(width, super.getSuggestedMinimumWidth());
    }

    @Override
    protected int getSuggestedMinimumHeight() {
	float density = getResources().getDisplayMetrics().density;
	int height = Math.round(32 * density);
	return Math.max(height, super.getSuggestedMinimumHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
	int left = getPaddingLeft();
	int top = getPaddingTop();
	int right = getWidth() - getPaddingRight();
	int bottom = getHeight() - getPaddingBottom();

	canvas.drawRect(left, top, right, bottom, getPaint1());
	canvas.drawRect(left + 1, top + 1, right - 1, bottom - 1, getPaint2());
	canvas.drawRect(left + 2, top + 2, right - 2, bottom - 2, mPaint3);
    }

    private static Paint getPaint1() {
	if (sPaint1Ref != null) {
	    Paint paint = sPaint1Ref.get();
	    if (paint != null) {
		return paint;
	    }
	}
	Paint paint = new Paint();
	paint.setColor(-1);
	sPaint1Ref = new WeakReference<Paint>(paint);
	return paint;
    }

    private static Paint getPaint2() {
	if (sPaint2Ref != null) {
	    Paint paint = sPaint2Ref.get();
	    if (paint != null) {
		return paint;
	    }
	}
	Paint paint = new Paint();
	paint.setColor(0xff000000);
	sPaint2Ref = new WeakReference<Paint>(paint);
	return paint;
    }
}
